<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工作名称" prop="jobName">
        <el-input
            v-model="queryParams.jobName"
            placeholder="请输入工作名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['nmzp:job:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="Edit"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['nmzp:job:edit']"
              >修改</el-button>
            </el-col>-->


      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jobList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="工作名称" align="center" prop="jobName"/>

      <el-table-column label="大概薪资，" align="center" prop="jobBudget"/>
      <el-table-column label="工作地点" align="center">
        <template #default="scope">
          {{ getCityName(scope.row.address) }}
        </template>
      </el-table-column>

      <el-table-column label="职位类型" align="center" prop="jobType"/>
      <el-table-column label="职位状态" align="center">
        <template #default="scope">
          {{ renderStatus(scope.row.status) }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button v-if="!examineShow" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['nmzp:job:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handlePop(scope.row)"
                     v-hasPermi="['nmzp:job:pop']">上/下线
          </el-button>
          <el-button link type="primary" icon="Operation" @click="handleJobList(scope.row)"
                     v-hasPermi="['nmzp:job:status']">简历集合
          </el-button>
          <el-button v-if="!examineShow" link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['nmzp:job:remove']">删除
          </el-button>
          <el-button v-if="examineShow&& scope.row.status == 0" link type="primary" icon="Edit"
                     @click="handleExamine(scope.row)"
                     v-hasPermi="['nmzp:job:edit']">
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改职位管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="jobRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工作名称" prop="jobName">
          <el-input v-model="form.jobName" placeholder="请输入工作名称" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="薪资" prop="jobBudget">
          <el-input v-model="form.jobBudget" placeholder="请输入薪资范围" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="工作地点" prop="address">
          <el-select v-model="form.address" placeholder="请选择工作地点" :disabled="isDisabled">
            <el-option v-for="city in cityOptions" :key="city.value" :label="city.label"
                       :value="city.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位类型" prop="jobType">
          <el-input v-model="form.jobType" placeholder="请输入职位类型" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="简介" prop="positionName">
          <el-input v-model="form.positionName" type="textarea" placeholder="请输入简介" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="工作要求" prop="jobAsk">
          <el-input v-model="form.jobAsk" type="textarea" placeholder="请输入工作要求" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="工作描述" prop="jobDescription">
          <el-input v-model="form.jobDescription" type="textarea" placeholder="请输入工作描述" :disabled="isDisabled"/>
        </el-form-item>
      </el-form>

      <template v-if="popButton" #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleUP(form)">上 线</el-button>
          <el-button type="primary" @click="handleDown(form)">下 线</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
      <template v-else-if="isDisabled" #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleExamineClickOk(form)">通 过</el-button>
          <el-button type="primary" @click="handleExamineClickNoOk(form)">不通过</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
      <template v-else #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <el-dialog :title="title" v-model="openResumeList" width="500px" append-to-body>
      <el-table :data="resumeList" style="width: 100%">
        <el-table-column prop="userName" label="用户名" width="120" />
        <el-table-column prop="resumeName" label="简历名称" width="120" />
        <el-table-column fixed="right" label="Operations" min-width="120">
          <template #default="scope">
            <el-button @click="downloadAttachment(scope.row)">去沟通</el-button>
            <el-button @click="downloadAttachment(scope.row)">查看简历</el-button>
          </template>

        </el-table-column>
      </el-table>
      <template  #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Job">
import {listJob, getJob, delJob, addJob, updateJob, examineJob, pop, jobResumeList} from "@/api/nmzp/job";

const {proxy} = getCurrentInstance();

const jobList = ref([]);
const open = ref(false);
const openResumeList = ref(false);

const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const examineShow = ref(false);//是否展示审核按钮
const isDisabled = ref(false); // 控制输入框是否禁用
const popButton = ref(false); // 上下线按钮选项

const cityOptions = [
  {label: "北京", value: "1"},
  {label: "上海", value: "2"},
  {label: "天津", value: "3"},
  {label: "重庆", value: "4"},
  {label: "广州", value: "5"},
  {label: "深圳", value: "6"},
  {label: "成都", value: "7"},
  {label: "武汉", value: "8"},
  {label: "南京", value: "9"},
  {label: "西安", value: "10"},
  {label: "杭州", value: "11"},
  {label: "青岛", value: "12"},
  {label: "大连", value: "13"},
  {label: "宁波", value: "14"},
  {label: "厦门", value: "15"},
  {label: "苏州", value: "16"},
  {label: "佛山", value: "17"},
  {label: "长沙", value: "18"},
  {label: "郑州", value: "19"},
  {label: "济南", value: "20"},
  {label: "哈尔滨", value: "21"},
  {label: "长春", value: "22"},
  {label: "沈阳", value: "23"},
  {label: "昆明", value: "24"},
  {label: "南宁", value: "25"},
  {label: "海口", value: "26"},
  {label: "兰州", value: "27"},
  {label: "乌鲁木齐", value: "28"},
  {label: "呼和浩特", value: "29"},
  {label: "石家庄", value: "30"},
  {label: "太原", value: "31"},
  {label: "合肥", value: "32"},
  {label: "南昌", value: "33"},
  {label: "福州", value: "34"},
  {label: "贵阳", value: "35"},
  {label: "西宁", value: "36"},
  {label: "拉萨", value: "37"},
  {label: "银川", value: "38"},
  {label: "澳门", value: "39"},
  {label: "香港", value: "40"}
];
const cityMap = {
  "1": "北京",
  "2": "上海",
  "3": "天津",
  "4": "重庆",
  "5": "广州",
  "6": "深圳",
  "7": "成都",
  "8": "武汉",
  "9": "南京",
  "10": "西安",
  "11": "杭州",
  "12": "青岛",
  "13": "大连",
  "14": "宁波",
  "15": "厦门",
  "16": "苏州",
  "17": "佛山",
  "18": "长沙",
  "19": "郑州",
  "20": "济南",
  "21": "哈尔滨",
  "22": "长春",
  "23": "沈阳",
  "24": "昆明",
  "25": "南宁",
  "26": "海口",
  "27": "兰州",
  "28": "乌鲁木齐",
  "29": "呼和浩特",
  "30": "石家庄",
  "31": "太原",
  "32": "合肥",
  "33": "南昌",
  "34": "福州",
  "35": "贵阳",
  "36": "西宁",
  "37": "拉萨",
  "38": "银川",
  "39": "澳门",
  "40": "香港"
};
const resumeList = ref([]);//简历集合

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    jobName: null,
    positionName: null,
    jobAsk: null,
    jobBudget: null,
    address: null,
    companyId: null,
    publishUserId: null,
    jobDescription: null,
    createUser: null,
    jobType: null,
    resumeId: null,
    idDel: null
  },
  rules: {},

});
const attachmentUrl=ref(null);//简历url
const {queryParams, form, rules} = toRefs(data);

/** 查询职位管理列表 */
function getList() {
  loading.value = true;
  listJob(queryParams.value).then(response => {
    jobList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
  let roles = localStorage.getItem('userRoles')
  if (roles && roles.includes('admin')) {
    examineShow.value = true;
  } else {
    // console.log('用户不具有 admin 角色');
  }
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
  isDisabled.value = false;
  popButton.value = false;
  openResumeList.value=false;

}

// 表单重置
function reset() {
  form.value = {
    jobId: null,
    jobName: null,
    positionName: null,
    jobAsk: null,
    jobBudget: null,
    address: null,
    companyId: null,
    publishUserId: null,
    jobDescription: null,
    createTime: null,
    updateTime: null,
    createUser: null,
    jobType: null,
    resumeId: null,
    idDel: null,
    status: null,
  };
  proxy.resetForm("jobRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.jobId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function getCityName(code) {
  return this.cityMap[code] || code;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加职位管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _jobId = row.jobId || ids.value
  getJob(_jobId).then(response => {
    form.value = response.data;
    form.value.address = cityMap[response.data.address];
    open.value = true;
    title.value = "修改职位管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["jobRef"].validate(valid => {
    if (valid) {
      if (form.value.jobId != null) {
        updateJob(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addJob(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _jobIds = row.jobId || ids.value;
  proxy.$modal.confirm('是否确认删除职位管理编号为"' + _jobIds + '"的数据项？').then(function () {
    return delJob(_jobIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/**
 * 审核按钮操作
 * @param row
 */
function handleExamine(row) {
  reset();
  const _jobId = row.jobId || ids.value
  getJob(_jobId).then(response => {
    form.value = response.data;
    form.value.address = cityMap[response.data.address];
    open.value = true;
    title.value = "职位审核";
  });
  isDisabled.value = true;
}

/**
 * 审核确认按钮
 * @param row
 */
function handleExamineClickOk(row) {
  const requestData = {
    jobId: row.jobId,
    status: 1//通过
  };
  examineJob(requestData).then(response => {
    proxy.$modal.msgSuccess("审核成功");
    open.value = false;
    getList();
  });
  isDisabled.value = false;

}

/**
 * 审核不确认按钮
 * @param row
 */
function handleExamineClickNoOk(row) {
  // 自定义数据结构体，只发送需要的字段
  const requestData = {
    jobId: row.jobId,
    status: 2//不通过
  };
  examineJob(requestData).then(response => {
    proxy.$modal.msgSuccess("审核成功");
    open.value = false;
    getList();
  });
  isDisabled.value = false;

}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('nmzp/job/export', {
    ...queryParams.value
  }, `job_${new Date().getTime()}.xlsx`)
}

function renderStatus(status) {
  switch (status) {
    case 0:
      return '未审核';
    case 1:
      return '审核通过';
    case 2:
      return '审核未通过';
    case 3:
      return '上线中';
    case 4:
      return '下线';
    default:
      return '未知状态';
  }
}

/** 上/下线按钮操作 */
function handlePop(row) {
  reset();
  const _jobId = row.jobId || ids.value
  getJob(_jobId).then(response => {
    form.value = response.data;
    form.value.address = cityMap[response.data.address];
    open.value = true;
    popButton.value = true;
    title.value = "上/下线";
  });
}

/**
 * 简历集合-操作按钮点击
 * @param row
 */
function handleJobList(row) {
  reset();
  const _jobId = row.jobId || ids.value


  //请求体
  const requestData = {
    jobId: _jobId,
  };
  jobResumeList(requestData).then(response => {
    resumeList.value = response.rows;
    openResumeList.value = true;
    console.log(resumeList)
    title.value = "当前职位简历列表";
  });
}
/**
 *
 * @param row
 */
function handleUP(row) {
  const requestData = {
    jobId: row.jobId,
    status: 3//上线
  };
  pop(requestData).then(response => {
    proxy.$modal.msgSuccess(response.msg);
    open.value = false;
    getList();
  });
  popButton.value = false;

}

function handleDown(row) {
  const requestData = {
    jobId: row.jobId,
    status: 4//上线
  };
  pop(requestData).then(response => {
    proxy.$modal.msgSuccess(response.msg);
    open.value = false;
    getList();
  });
  popButton.value = false;

}


const downloadAttachment = (row) => {
  const url = `http://localhost:8080${row.attachment}`;
  window.open(url, '_blank'); // 使用 window.open 打开新窗口
};
getList();
</script>
